<?php
namespace app\reduction\controller;
use app\AdminRest;
use app\dynamic\model\CardUser;
use app\dynamic\model\UserInfo;
use think\App;
use app\reduction\model\Active as Model;
use app\reduction\model\Company;
use app\reduction\model\UserRecord;

use think\facade\Cache;
use think\facade\Db;

class AdminActive extends AdminRest
{

    protected $model;

    protected $company_model;

    protected $user_record_model;

    public function __construct(App $app) {
        parent::__construct($app);
        $this->model         = new Model();

        $this->company_model = new Company();

        $this->user_record_model = new UserRecord();
    }


    /**
     * User: chenniang
     * Date: 2019-12-18 14:30
     * @return void
     * descrption:列表
     */
    public function activeList(){
       $input = $this->_input;
        //uniacid
       $dis[] = ['uniacid','=',$this->_uniacid];
        //查询没有被删除对
       $dis[] = ['status','>',-1];
       //查询
       if(!empty($input['title'])){
           $dis[] = ['title','like',"%".$input['title']."%"];
       }
        //查询
       $data  = $this->model->activeList($dis,$input['limit']);

        return $this->success($data);
    }

    /**
     * @author chenniang
     * @DataTime: 2020-04-10 09:20
     * @功能说明:活动详情
     */
    public function activeInfo(){

        $input = $this->_input;

        $data  = $this->model->activeInfo(['id'=>$input['id']]);

        $dis = [
            //uniacid
            'a.uniacid' => $this->_uniacid,
            //活动id
            'a.active_id'=> $input['id']
        ];
        //公司
        $data['company'] = $this->company_model->getId($dis);

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-03-09 18:48
     * @功能说明:添加活动
     */
    public function activeAdd(){

        $input = $this->_input;
        //转换格式
        $input = $this->changeData($input);
        //添加
        $res   = $this->model->activeAdd($input);

        return $this->success($res);
    }

    /**
     * @author chenniang
     * @DataTime: 2020-03-09 18:49
     * @功能说明:转换数据
     */
    public function changeData($input){
        //uniacid
        $input['uniacid'] = $this->_uniacid;

        //公司
        if(!empty($input['company'])){

            $input['company'] =  $this->company_model->changeData($input['company']);
        }
        //判断满减
        if($input['full']<=$input['reduction']){

            $this->errorMsg('优惠金额不能大于等于最低消费金额');
        }

        if(!empty($input['id'])&&!empty($input['get_num'])&&$input['get_num']>$input['num']){

            $this->errorMsg('满减券总量不得小于被领取数量,已被领取'.$input['get_num'].'张');
        }

        if(isset($input['get_num'])){

            unset($input['get_num']);
        }

        if(isset($input['use_num'])){

            unset($input['use_num']);
        }

        if(isset($input['company_num'])){

            unset($input['company_num']);
        }



        return $input;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-21 11:26
     * @功能说明:编辑
     */
    public function activeUpdate(){

        $input = $this->_input;
        //转换格式
        $input = $this->changeData($input);
        //添加
        $res   = $this->model->adminActiveUpdate(['id'=>$input['id']],$input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-21 15:33
     * @功能说明:停止发放
     */
    public function stopActive(){

        $input = $this->_input;

        $res   = $this->model->activeUpdate(['id'=>$input['id']],['status'=>$input['status']]);

        return $this->success($res);

    }

    /**
     * User: chenniang
     * Date: 2019-11-29 17:28
     * @return void
     * descption:获取员工
     */
    public function getStaffInfo(){
        $input  = $this->_input;

        $active = $this->model->activeInfo(['id'=>$input['active_id']]);

        if($active['use_condition']==1){

            $compay_id = $this->company_model->where(['active_id'=>$active['id']])->column('company_id');

            $dis[] = ['company_id','in',$compay_id];
        }

        $user_model = new UserInfo();

        $user_info  = new CardUser();

        $dis[] = ['is_staff','=',1];

        $dis[] = ['uniacid','=',$this->_uniacid];

        if(!empty($input['name'])){
            $dis[]  = ['name','like',"%".$input['name']."%"];
        }
        $data = $user_model->where($dis)->field('fans_id as id,name')->select();
        if(!empty($data)){

            foreach ($data as $k=>&$v){

                $is_staff = $user_info->where(['id'=>$v['id'],'is_staff'=>1])->find();


                if(empty($is_staff)){
                    unset($data[$k]);
                    continue;
                }

                if(empty($v['name'])){

                    $v['name'] = $user_info->where(['id'=>$v['id']])->value('nickName');
                }
            }
        }
        return $this->success($data);
    }


    /**
     * User: chenniang
     * Date: 2019-09-18 18:38
     * @return void
     * descption:福包分享码
     */
    public function couponShareQr(){

        $input = $this->_param;

        $coupon= $this->model->activeInfo(['id'=>$input['id']]);

        if(empty($coupon)){
            return $this->error('no data');
        }
        $input['pid'] = $input['staff_id'];

        $coupon_qr    = getCache($input['id'].'-'.$input['staff_id'].'-reduction_qr',$this->_uniacid);

        if(empty($coupon_qr)){

            $data = longbingCreateWxCode($this->_uniacid,$input,$input['page']);

            if(isset($data['qr_path'])) $data = transImagesOne($data ,['qr_path'] ,$this->_uniacid);

            $coupon_qr = $data['qr_path'];

            setCache($input['id'].'-'.$input['staff_id'].'-reduction_qr',$coupon_qr,86400,$this->_uniacid);
        }
        return $this->success($coupon_qr);
    }


}
